package com.yunti.springboot.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.commonenum.SubjectEnum;
import com.yunti.springboot.mapper.ElectiveSubMapper;
import com.yunti.springboot.mapper.SubjectMapper;
import com.yunti.springboot.po.ElectiveSub;
import com.yunti.springboot.po.Subject;
import com.yunti.springboot.service.ElectiveSubService;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.ElectiveSubReq;
import com.yunti.springboot.vo.res.ElectiveSubRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ytt
 * @since 2023-06-25
 */
@Service
public class ElectiveSubServiceImpl extends ServiceImpl<ElectiveSubMapper, ElectiveSub> implements ElectiveSubService {
    @Resource
    ElectiveSubMapper mapper;
    @Resource
    SubjectMapper subjectMapper;

    @Override
    public Result getElectiveSubList(ElectiveSubReq req) {
        LambdaQueryWrapper<ElectiveSub> queryWrapper = new LambdaQueryWrapper<>();
        if (req.getCurrent() == null) {
            req.setCurrent(0);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        if (req.getId() != null) {
            queryWrapper.eq(ElectiveSub::getId, req.getId());
        }
        if (req.getSemester() != null) {
            queryWrapper.eq(ElectiveSub::getSemester, req.getSemester());
        }
        if (req.getType() != null) {
            queryWrapper.eq(ElectiveSub::getType, req.getType());
        }
        if (req.getSex() != null) {
            queryWrapper.eq(ElectiveSub::getSex, req.getSex());
        }
        Page page = new Page<>(req.getCurrent(), req.getSize());
        mapper.selectPage(page, queryWrapper);
        List<ElectiveSub> list = page.getRecords();
        List<ElectiveSubRes> subList = new LinkedList<>();
        for (ElectiveSub sub : list) {
            ElectiveSubRes res = new ElectiveSubRes();
            BeanUtil.copyProperties(sub, res);
            if (res.getSubId() != null) {
                if (res.getSubId() == -1) {
                    res.setSubName("选课科目");
                } else {
                    Subject subject = subjectMapper.selectById(res.getSubId());
                    if(Objects.isNull(subject)){
                        res.setSubName("项目不存在，【ID】:res.getSubId()");
                    }else {
                        res.setSubName(subject.getName());
                    }

                }
            }
            subList.add(res);
        }
        page.setRecords(subList);
        return Result.success(page);
    }

    @Override
    public Result add(ElectiveSubReq req) {
        ElectiveSub sub = new ElectiveSub()
                .setSubId(req.getSubId())
                .setCreateTime(new Date())
                .setType(req.getType())
                .setSemester(req.getSemester())
                .setSex(req.getSex());
        if (mapper.insert(sub) > 0)
            return Result.success("新增成功！");
        return Result.error("新增失败！");
    }

    @Override
    public Result del(ElectiveSubReq req) {
        if (mapper.deleteById(req.getId()) > 0)
            return Result.success("删除成功！");
        return Result.error("删除失败！");
    }

    @Override
    public Result edit(ElectiveSubReq req) {
        if (req.getId() == null) {
            return Result.error("id不能为空");
        }
        ElectiveSub sub = new ElectiveSub()
                .setSubId(req.getSubId())
                .setId(req.getId())
                .setUpdateTime(new Date())
                .setType(req.getType())
                .setSemester(req.getSemester())
                .setSex(req.getSex());
        if (mapper.updateById(sub) > 0)
            return Result.success("更改成功！");
        return Result.error("更改失败！");
    }


}
